/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.figures;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.ContainerFigure;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Defines the view of an instance specification.
 * 
 * @author Gilles Cannenterre
 * @since 17 janv. 07
 */
public class InstanceSpecificationFigure extends NodeFigure {
	
	/**
	 * 
	 */
	protected static final int BORDER_MARGIN = 5;

	/**
	 * The border figure.
	 */
	protected BorderFigure borderFigure;

	/**
	 */
	protected Figure contentPane;

	/**
	 */
	protected ContainerFigure slotsContent;

	/**
	 */
	protected boolean isBorderRequired = false;

	/**
	 * The is name underlined.
	 */
	protected boolean isNameUnderlined = false;

	/**
	 * The Class ActorLabelFigure.
	 * Holds the names and stereotypes.
	 */
	protected class BorderFigure extends Figure {
	
		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){
			if (isBorderRequired){
				setForegroundColor(getBorderColor());
				Point topLeft = getBounds().getTopLeft();
				Point topRight = getBounds().getTopRight();
				graphics.drawLine(topLeft.x,topLeft.y + BORDER_MARGIN,topRight.x,topRight.y + BORDER_MARGIN);
			}	
		}
	
		/**
		 * The Constructor.
		 */
		public BorderFigure() {
			super();
			setSize(new Dimension(10,2*BORDER_MARGIN));
		}
	}


	/**
	 * The Constructor.
	 */
	public InstanceSpecificationFigure() {
		super();
		ToolbarLayout layout = new ToolbarLayout(ToolbarLayout.VERTICAL);
		layout.setStretchMinorAxis(true);
		layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
		setLayoutManager(layout);
		setOpaque(true);
		borderFigure = new BorderFigure();
		add(borderFigure);
		setNameUnderlined(true);
		createContentPane();
	}
	
	/**
	 * Refresh colors.
	 * 
	 * @param graphElement the graph element
	 */
	@Override
	public void refreshColors(GraphElement graphElement) {
		super.refreshColors(graphElement);
		
		Font baseFont = super.getNameLabel().getFont();
		if (baseFont.getFontData() != null){
			FontData baseFontData = baseFont.getFontData()[0];
			FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.BOLD)};
			Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);	
			super.setFont(labelFont);
		}
		
		borderFigure.setForegroundColor(getBorderColor());
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public BorderFigure getBorderFigure() {
		return borderFigure;
	}

	/**
	 * 
	 */
	protected void createContentPane() {
		contentPane = new Figure();
		ToolbarLayout layout = new ToolbarLayout();
		layout.setVertical(true);
		contentPane.setLayoutManager(layout);
		add(contentPane);	
	}

	/**
	 * 
	 * 
	 * @param b 
	 */
	public void setAbstract(boolean b){
		Font baseFont = super.getNameLabel().getFont();
	
		if (b) {
			if (baseFont.getFontData() != null){
				FontData baseFontData = baseFont.getFontData()[0];
				FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.ITALIC|SWT.BOLD)};
				Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);	
				super.setFont(labelFont);
			}
		} else {
			if (baseFont.getFontData() != null){
				FontData baseFontData = baseFont.getFontData()[0];
				FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.BOLD)};
				Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);	
				super.setFont(labelFont);
			}
		}
	}

	/**
	 * 
	 * 
	 * @param isBorderRequired 
	 */
	public void setBorderRequired(boolean isBorderRequired) {
		this.isBorderRequired = isBorderRequired;
	}

	/**
	 * 
	 * 
	 * @param rect 
	 */
	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		Dimension labelFigureDimension = getNameLabel().getPreferredSize();
		Dimension borderFigureDimension = getBorderFigure().getPreferredSize();
		if (slotsContent != null) {
			slotsContent.setPreferredSize(rect.width, rect.height - labelFigureDimension.height - borderFigureDimension.height);
		}
	}

	/**
	 * Paint border.
	 * 
	 * @param graphics the graphics
	 */
	@Override
	public void paintBorder(Graphics graphics){
		super.paintBorder(graphics);
		if (isNameUnderlined) {
			graphics.setForegroundColor(getFontColor());
			Rectangle rectLabel = getNameLabel().getBounds();
			Dimension labelSize=FigureUtilities.getTextExtents(getNameLabel().getText(), getNameLabel().getFont());
			graphics.drawLine(rectLabel.x+rectLabel.width/2-labelSize.width/2,
					rectLabel.y+rectLabel.height+1,
					rectLabel.x+rectLabel.width/2+labelSize.width/2,
					rectLabel.y+rectLabel.height+1);
		}
	}

	/**
	 * 
	 * 
	 * @param isNameUnderlined 
	 */
	public void setNameUnderlined(boolean isNameUnderlined) {
		this.isNameUnderlined = isNameUnderlined;
	}

	/**
	 * Get the inner figure accepting childs figure.
	 * 
	 * @return 
	 */
	public IFigure getContentPane() {
		return contentPane;	
	}
}